package com.kaka.bus.engine.borrow.pact;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.api.RetrofitUtils;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.util.ToastUtil;
import com.kaka.bus.util.U;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/12/1.
 */

public class PactGreementActivity extends RxBaseActivity {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.webview_progressbar)
    ProgressBar mWebviewProgressbar;
    @BindView(R.id.webview)
    WebView mWebview;
    private String mBorr_ids;

    public static final String TYPE = "type";
    public static final String URL = "url";
    public static final String TITLE = "title";
    public static final String TYPE_LOAD_URL = "load_url";
    private String mUrl;

    @Override
    protected void initView() {
        U.initWebView(mWebview, mWebviewProgressbar);
        mToolbarBack.setOnClickListener(v -> finish());

        Intent intent = getIntent();
        if (intent != null) {
            String type = intent.getStringExtra(TYPE);
            if (TYPE_LOAD_URL.equals(type)) {
                mUrl = intent.getStringExtra(URL);
                String title = intent.getStringExtra(TITLE);
                mToolbarName.setText(title);
                loadUrl();
            } else {
                mToolbarName.setText("电子合同");
                mBorr_ids = intent.getStringExtra("borr_id");
                getInfo();
            }
        }
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    private void loadUrl() {
        if (mUrl != null) {
            mWebview.loadUrl(mUrl);
        }
    }

    private void getInfo() {
        if (mBorr_ids != null) {
            String userId = getUserId();
            String token = getToken();
            addSubscription(RetrofitUtils.getInstance().build()
                    .getContract(userId, mBorr_ids, token, U.Sign(userId, mBorr_ids, token))
                    .compose(T.D())
                    .subscribeWith(new Result<HttpWrapper<String>>() {
                        @Override
                        protected void onSuccess(HttpWrapper<String> data) {
                            if (Constant.SUCCESS_CODE.equals(data.code)) {
                                initData(data.data);
                            } else if (Constant.ERROR_CODE.equals(data.code)) {
                                ToastUtil.showShort(PactGreementActivity.this, data.info);
                            } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
                                U.tokenOut(PactGreementActivity.this);
                            }
                        }
                    }));
        }
    }

    private void initData(String data) {
        mWebview.loadData(data, "text/html; charset=UTF-8", null);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

}
